"ui";
const { commandClick, commandScreenshot, commandSwipeLeft, commandSwipeRight, commandSwipeUp, commandSwipeDown, commandOpenApp, commandClickByCoordinate, commandBack, commandHome} = require('./order-script/index.js')

ui.statusBarColor("#1d324f");
ui.layout(
  <vertical w="match_parent" h="match_parent">
    <webview id="web" w="*" h="*" />
  </vertical>
)


// ui.web.loadUrl('http://192.168.1.95:8150/')

requestScreenCapture()


let scriptInfo = [
  {
    name: "截图",
    cmd: commandScreenshot
  },
  {
    name: '点击',
    cmd: commandClick
  },
  {
    name: '左滑',
    cmd: commandSwipeLeft
  },
  {
    name: '右滑',
    cmd: commandSwipeRight
  },
  {
    name: '上滑',
    cmd: commandSwipeUp
  },
  {
    name: '下滑',
    cmd: commandSwipeDown
  },
  {
    name: '打开',
    cmd: commandOpenApp
  },
  {
    name: '坐标',
    cmd: commandClickByCoordinate
  },
  {
    name: '返回',
    cmd: commandBack
  },
  {
    name: '桌面',
    cmd: commandHome
  }
]

events.observeNotification();
events.onNotification(function(notification){
  let text = notification.getText();
  if('com.tencent.mm' !== notification.getPackage())return;
  let bool = /cmd\s.+/.test(text);
  if(!bool)return;
  let cmd = text.replace(/cmd\s/,'').split(' ');

  notification.delete();
  for (let index = 0; index < scriptInfo.length; index++) {
    const element = scriptInfo[index];
    if(cmd[0] == element.name){
      element.cmd(cmd[1], cmd[2], cmd[3]);
      break;
    }
  }

});